package cn.chencaiju.day03;

import java.util.Arrays;

import org.junit.Test;

public class ShuffleArray {
	//原数组改变
	public void shuffleArray(int[] nums) {
		int j=0,temp=0;
		for(int i=0;i<nums.length;i++) {
			j=(int) (Math.random()*nums.length);
			temp=nums[j];
			nums[j]=nums[i];
			nums[i]=temp;
		}
		System.out.println(Arrays.toString(nums));		
	}
	//原数组不变
	public int[] shuffleNewArray(int[] nums) {
		int len=nums.length;
		int[] newArray=new int[len];
		for(int i=0;i<newArray.length;i++) {
			newArray[i]=nums[i];
		}
		int j=0,temp=0;
		for(int i=0;i<newArray.length;i++) {
			j=(int) (Math.random()*newArray.length);
			temp=newArray[j];
			newArray[j]=newArray[i];
			newArray[i]=temp;
		}
		System.out.println("打乱数组顺序："+Arrays.toString(newArray));		
		return newArray;
	}
	@Test public void testShuffleArray() {
		int[] nums=new int[10];
		for(int i=0;i<nums.length;i++) {
			nums[i]=(int) (Math.random()*100);
		}
		Arrays.sort(nums);
		System.out.println("原数组为："+Arrays.toString(nums));
		shuffleArray(nums);
	}
	@Test public void testShuffleNewArray() {
		int[] nums=new int[10];
		for(int i=0;i<nums.length;i++) {
			nums[i]=(int) (Math.random()*100);
		}
		Arrays.sort(nums);
		System.out.println("原数组为："+Arrays.toString(nums));
		shuffleNewArray(nums);
		System.out.println("打乱顺序后，原数组："+Arrays.toString(nums));
	}
}
